package edu.jmu.dustbinMonitoringSystem.web;

import edu.jmu.dustbinMonitoringSystem.dto.AbstractBaseDomain;
import edu.jmu.dustbinMonitoringSystem.dto.AbstractBaseResult;
import edu.jmu.dustbinMonitoringSystem.dto.BaseResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Wen
 */
public abstract class AbstractBaseController<T extends AbstractBaseDomain> {
    protected HttpServletResponse response;
    protected HttpServletRequest request;

    @Autowired
    ConfigurableApplicationContext applicationContext;

    @ModelAttribute
    public void initReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }
    protected AbstractBaseResult success(T attribute ) {
        return BaseResultFactory.getInstance(response,request).build(attribute);
    }

    protected AbstractBaseResult success(int next, int last, List<T> attributes) {
        return BaseResultFactory.getInstance(response,request).build(next,last,attributes);
    }

    protected AbstractBaseResult error(int code,String title,String detail){
        response.setStatus(code);
        return BaseResultFactory.getInstance(response,request).build(code,title,detail,"DEBUG");
    }

}

